package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: health_parent
 * @description:
 * @author: HuChunping
 * @create: 2021-01-06 19:21
 **/
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    /**
    * @Description: 订阅检查项服务
    * @Reference注入的是分布式中的远程服务对象， @Autowired注入的是本地spring容器中的对象。
    */
    @Reference
    private CheckItemService checkItemService;

    @GetMapping("/findAll")
    public Result findAll(){
        // 调用检查项的查询所有服务
        List<CheckItem> list = checkItemService.findAll();
        // 封装到Result中返回
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
    }

    /**
    * @Description: 新增检查项
    * @Param: [checkItem]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/7-15:46
    */
    // @requestBody可以将请求体中的JSON字符串绑定到相应的bean上，如下指明绑定到CheckItem上
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        checkItemService.add(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
    * @Description: 分页查询
    * @Param: [queryPageBean]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/7-21:19
    */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<CheckItem> pageResult = checkItemService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    // 形参id和?id=。请求参数中的id一样，可以不用@requestparam取别名
    /**
    * @Description: 通过id查询检查项
    * @Param: [id]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/7-21:18
    */
    @GetMapping("/findById")
    public Result findById(int id){
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
    * @Description: 更新检查项
    * @Param: [checkItem]
    * @return: com.itheima.health.entity.Result
    * @Author: HuChunping
    * @Date: 2021/1/7-21:14
    */
    @PostMapping("/update")
    public Result update(@RequestBody CheckItem checkItem){
        checkItemService.update(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @PostMapping("/deleteById")
    public Result deleteById(int id){
        checkItemService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
}
